﻿using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Serialization;
using BizControl.DalLinq;
using BizControl.Library.BusinessObjects.Models;
using BizControl.Library.Security;
using System.Security;
using BizControl.Resources;

namespace BizControl.Library.BusinessObjects.People
{
  [Serializable()]
  public class PersonList : ReadOnlyListBase<PersonList, PersonInfo>
  {
    public void AllowChange()
    {
      IsReadOnly = false;
    }

    public void ForbidChange()
    {
      IsReadOnly = true;
    }

    public PersonInfo this[Guid personIdSearchFor]
    {
      get
      {
        foreach (PersonInfo person in this)
        {
          if (person.Id == personIdSearchFor)
            return person;
        }
        return null;
      }
    }

    public PersonInfo this[PersonInfo personSearchFor]
    {
      get
      {
        foreach (PersonInfo person in this)
        {
          if (person.Id == personSearchFor.Id)
            return person;
        }
        return null;
      }
    }

    #region  Factory Methods

    public static PersonList NewPersonList()
    {
      return DataPortal.Create<PersonList>();
    }

    public static PersonList GetPersonList()
    {
      return DataPortal.Fetch<PersonList>();
    }

    public static PersonList GetPersonList(string lastName)
    {
      return DataPortal.Fetch<PersonList>(new SingleCriteria<PersonList, string>(lastName));
    }

    private PersonList()
    { /* require use of factory methods */ }

    #endregion

    #region  Data Access

    private void DataPortal_Create()
    {

    }

    private void DataPortal_Fetch()
    {
      // fetch with no filter
      Fetch("");
    }

    private void DataPortal_Fetch(SingleCriteria<PersonList, string> criteria)
    {
      Fetch(criteria.Value);
    }

    private void Fetch(string nameFilter)
    {
      RaiseListChangedEvents = false;
      var currentModel = SecurityChecks.GetCurrentModel();
      SecurityChecks.GetCurrentUser();
      using (var ctx = ContextManager<BusinessControl2DataContext>.GetManager(Database.BizControlDB))
      {
        var data = from p in ctx.DataContext.People
                   from pm in ctx.DataContext.PersonModels
                   where !p.IsDeleted && pm.PersonId == p.PersonId && pm.ModelId == currentModel.ModelId
                   orderby p.LastName
                   select new PersonInfo(p.PersonId, p.FirstName, p.LastName, p.Photo);
        if (!(string.IsNullOrEmpty(nameFilter)))
        {
          data = from p in ctx.DataContext.People
                 from pm in ctx.DataContext.PersonModels
                 where p.LastName.Contains(nameFilter) && !p.IsDeleted && pm.PersonId == p.PersonId && pm.ModelId == currentModel.ModelId
                 orderby p.LastName
                 select new PersonInfo(p.PersonId, p.FirstName, p.LastName, p.Photo);
        }
        IsReadOnly = false;
        this.AddRange(data);
        IsReadOnly = true;
      }
      RaiseListChangedEvents = true;
    }

    #endregion

  }
}
